(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('companyRatingService', companyRatingService);
	companyRatingService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory' ];
	function companyRatingService($q, loggerFactory, commonService, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("companyRatingService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getLayoutForCompanyRating : {
				url : 'do/cda/companyRating/getLayoutForCompanyRating',
				method : 'post'
		}});
		function getLayoutForCompanyRating(param) {
			var defer = $q.defer();
			ajaxService.getLayoutForCompanyRating(param).then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function initRatingTypeQaItemAndQaAdj(ctrl, layoutConfig, isViewPage) {
			var oldCompanyRating = angular.copy(ctrl.companyRating);
			ctrl.ratingTypeOptions = ctrl.companyRating.ratingTypeOptions || [];
			if("Y" == isViewPage) ctrl.companyRating.isViewPage = "Y";
			getLayoutForCompanyRating(ctrl.companyRating).then(function(response) {
				layoutConfig.formConfigs.ratingQaItems = {};
				layoutConfig.formConfigs.ratingQaAdjs = {};
				ctrl.layoutConfig = angular.merge(layoutConfig, response.data);
				//need to reset the ctrl.companyRating solve the issue ratingDate is null. or other field.
				ctrl.companyRating = oldCompanyRating;
			}, angular.noop);
			
			ctrl.qaItemMap = {};
			ctrl.qaItemMap = ctrl.companyRating.voRatingType.qaItemMap;
			//private Map<String, VoRatingQaItem> qaItemMap = new HashMap<String, VoRatingQaItem>();
			angular.forEach(ctrl.qaItemMap, function(ratingQaItemObj, index1) {
				var itemType = ratingQaItemObj.itemType;
				var isCategory = ratingQaItemObj.isCategory;
				if("Y"!=isCategory) {
					if("O" != itemType && "I" != itemType) {
						ctrl.qaItemMap[index1].checkboxChanged = function(){
							ctrl.qaItemMap[index1].usedItemType = 'O';
							if(ctrl.qaItemMap[index1].itemValues != null && ctrl.qaItemMap[index1].itemValues.length == 0 && ctrl.qaItemMap[index1].itemValue == ''){
								ctrl.qaItemMap[index1].readonlyFlag = false;
								ctrl.qaItemMap[index1]['readonlyFlag'+'_C'] = false;
							} else if(ctrl.qaItemMap[index1].itemValues != null && ctrl.qaItemMap[index1].itemValues.length > 0) {
								ctrl.qaItemMap[index1].readonlyFlag = true;
								ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = false;
							} else {
								ctrl.qaItemMap[index1].readonlyFlag = false;
								ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = true;
							}
						};
						ctrl.qaItemMap[index1].textChanged = function(){
							ctrl.qaItemMap[index1].usedItemType = 'I';
							if(ctrl.qaItemMap[index1].itemValues != null && ctrl.qaItemMap[index1].itemValues.length == 0 && ctrl.qaItemMap[index1].itemValue == ''){
								ctrl.qaItemMap[index1].readonlyFlag = false;
								ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = false;
							}else if(ctrl.qaItemMap[index1].itemValue != '') {
								ctrl.qaItemMap[index1].readonlyFlag = false;
								ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = true;
							} else {
								ctrl.qaItemMap[index1].readonlyFlag = true;
								ctrl.qaItemMap[index1]['readonlyFlag'+'_C']  = false;
							}
						};
					} else {
						ctrl.qaItemMap[index1].checkboxChanged = function(){
							ctrl.qaItemMap[index1].usedItemType = 'O';
						};
						ctrl.qaItemMap[index1].textChanged = function(){
							ctrl.qaItemMap[index1].usedItemType = 'I';
						};
					}
					ctrl.qaItemMap[index1].dataOptions = ratingQaItemObj.itemOptions;
				}
				ctrl.qaItemMap[index1].dataOptions = ratingQaItemObj.itemOptions;
			});
			ctrl.qaAdjMap = {};
			ctrl.qaAdjMap = ctrl.companyRating.voRatingType.qaAdjMap;
			angular.forEach(ctrl.qaAdjMap, function(ratingQaAdjObj, index1) {
				ctrl.qaAdjMap[index1].dataOptions = ratingQaAdjObj.adjOptions;
			});
		}
		return {
			getLayoutForCompanyRating : getLayoutForCompanyRating,
			initRatingTypeQaItemAndQaAdj : initRatingTypeQaItemAndQaAdj,
		};
	}
})(angular);